package com.ruoyi.app.service.impl

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Fail
import com.ruoyi.app.common.Ok
import com.ruoyi.app.domain.vo.SpreadCenterBannerVo
import com.ruoyi.app.domain.vo.SpreadCenterVo
import com.ruoyi.app.service.SpreadCenterApiService
import com.ruoyi.back.domain.SpreadCenter
import com.ruoyi.back.service.SpreadCenterService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class SpreadCenterApiServiceImpl : SpreadCenterApiService {

    @Autowired
    private lateinit var spreadCenterService: SpreadCenterService

    override fun getSpreadCenterBannerList(): Data<List<SpreadCenterBannerVo>> =
        spreadCenterService.list(
            KtQueryWrapper(SpreadCenter::class.java)
                .eq(SpreadCenter::location, 1)
                .eq(SpreadCenter::isShow, true)
                .orderByDesc(SpreadCenter::orderNum)
        )
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取推广中心轮播图成功", map {
                    SpreadCenterBannerVo().apply { BeanUtil.copyProperties(it, this) }
                })
            } ?: Ok("暂无数据", null)

    override fun getSpreadCenterAppList(type: Int): Data<List<SpreadCenterVo>> =
        spreadCenterService.list(
            KtQueryWrapper(SpreadCenter::class.java)
                .eq(SpreadCenter::location, type)
                .eq(SpreadCenter::isShow, true)
                .orderByDesc(SpreadCenter::orderNum)
        )
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取推广中心app成功", map {
                    SpreadCenterVo().apply { BeanUtil.copyProperties(it, this) }
                })
            } ?: Ok("暂无数据", null)
}